package com.mz.mattress.admin.api.merchant;

import com.mz.mattress.admin.common.Holder;
import com.mz.mattress.admin.service.MemberMerService;
import com.mz.mattress.api.common.Constants;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.jwt.MerchantJwt;
import com.mz.mattress.api.req.MobileLoginReq;
import com.mz.mattress.api.req.WxLoginReq;
import com.mz.mattress.api.resp.UserAuth;
import com.mz.mattress.api.util.IpUtil;
import com.mz.mattress.api.util.JacksonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping(Constants.MERCHANT_PATH + "auth")
public class AuthController {

    @Autowired
    private MemberMerService memberMerService;

//    @Autowired
//    private SmsService smsService;

    //微信登录，注册用户微信信息
    @PostMapping("wxLogin")
    public Result<UserAuth> wxLogin(@RequestBody WxLoginReq req, HttpServletRequest request) {
        log.info("WxLogin_req_params={}", JacksonUtils.toJson(req));
        String ip = IpUtil.client(request);
        req.setIp(ip);
        return memberMerService.wxLogin(req);
    }

    @PostMapping("bindWxMobile")
    public Result<UserAuth> bindWxMobile(@RequestBody String body) {
        // DONE 注册微信手机号
        MerchantJwt merchantJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        String sessionKey = merchantJwt.getSessionKey();
        Long memberId = merchantJwt.getMemberMerId();
        return memberMerService.bindWxMobile(sessionKey, body, memberId);
    }

    //微信登录，注册用户微信信息
    @PostMapping("mobileLogin")
    public Result<UserAuth> mobileLogin(@RequestBody MobileLoginReq req, HttpServletRequest request) {
        log.info("MobileLogin_req_params={}", JacksonUtils.toJson(req));
        String ip = IpUtil.client(request);
        req.setIp(ip);
        return memberMerService.mobileLogin(req);
    }

//    //发送短信验证码
//    @PostMapping("sendSmsCode")
//    public Result<String> sendSmsCode(@RequestBody String body) {
//        JsonNode jsonNode = JacksonUtils.toObj(body);
//        String mobile = jsonNode.get("mobile").textValue();
//        return smsService.sendSmsCode(mobile, SmsEnum.BIND_MOBILE);
//    }
//
//    @PostMapping("bindMobile")
//    public Result<UserAuth> bindMobile(@RequestBody BindMobileReq req, HttpServletRequest request) {
//        MerchantJwt merchantJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
//        Long memberId = merchantJwt.getMemberMerId();
//
//        return memberMerService.bindMobile(req, memberId);
//    }

}
